package com.seek.service.imp;

import com.seek.core.BaseServiceImpl;
import com.seek.dao.RegionInfoDao;
import com.seek.model.entity.TRegionInfo;
import com.seek.service.RegionInfoService;
import com.seek.util.StringUtil;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by weh on 2017/7/7.
 */
@Repository("regionInfoService")
public class RegionInfoServiceImpl extends BaseServiceImpl<TRegionInfo> implements
    RegionInfoService {


  @Autowired
  public void setDao(RegionInfoDao regionInfoDao) {
    super.setDao(regionInfoDao);
  }

  public RegionInfoDao getDao() {
    return (RegionInfoDao) super.getDao();
  }

  @Override
  public List<TRegionInfo> selectRegionByRegionIds(Object... ids) {
    if (null != ids && ids.length > 0) {
      StringBuffer params = new StringBuffer();
      for (int i = 0; i < ids.length; i++) {
        params.append("?,");
      }
      String content = params.toString();
      content = StringUtils.substring(content, 0, content.length() - 1);
      String sql = "select * from t_region_info where region_id in (" + content + ")";
      return this.getDao().findBySql(sql, ids);

    }

    return null;
  }

  @Override
  public String getCityByCardNo(String cardNo) {
    String pcode = StringUtils.substring(cardNo, 0, 2) + "0000";//省编码
    String ccode = StringUtils.substring(cardNo, 0, 4) + "00";//市编码
    List<TRegionInfo> regionInfos = this.selectRegionByRegionIds(new Object[]{pcode, ccode});

    if (CollectionUtils.isNotEmpty(regionInfos)) {
      String pname = "";//省名称
      String cname = "";//市名称
      for (TRegionInfo regionInfo : regionInfos) {
        if (regionInfo.getRegionId().endsWith("0000")) {
          pname = regionInfo.getRegionName();
        } else {
          cname = regionInfo.getRegionName();
        }
      }
      return pname + cname;
    }
    return null;
  }
}
